//
//  ImageViewController.m
//  engineeringDrawing
//
//  Created by BJB0004 on 17/4/1.
//  Copyright © 2017年 HangzhouBaixiongtechnologyco.,LTD. All rights reserved.
//

#import "ImageViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ImageViewController (){

    float scal;

}


@end

@implementation ImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor =[UIColor yellowColor];
    
      UIView * myview =[[UIView alloc]initWithFrame:CGRectMake(15, 60, self.view.bounds.size.width-30, self.view.bounds.size.height-100)];
    myview.backgroundColor =[UIColor greenColor];
    
    UIButton * btn =[[UIButton alloc]initWithFrame:CGRectMake(20, 20, 60, 30)];
    
    
    [btn setTitle:@"返回" forState:UIControlStateNormal];
    
    [btn setTintColor:[UIColor blackColor]];
    
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:myview];
    [self.view addSubview:btn];
    
    UIImageView * imageV =[[UIImageView alloc]init];
    
    if (self.h>[UIScreen mainScreen].bounds.size.height-100 ||self.w>[UIScreen mainScreen].bounds.size.width-30 ) {
        float hh = self.h/([UIScreen mainScreen].bounds.size.height-100);
        float ww =self.w / ([UIScreen mainScreen].bounds.size.width-30);
        
        float aa = hh>ww?hh:ww;
        
        
        imageV.frame =CGRectMake((WIDTH-30-self.w/aa)/2,(HEIGHT-100-self.h/aa)/2 , self.w/aa, self.h/aa);
       
        
    }else{
        int aa =1;
     imageV.frame =CGRectMake((WIDTH-30-self.w/aa)/2,(HEIGHT-100-self.h/aa)/2 , self.w/aa, self.h/aa);
        
    }
   
    
    imageV.image =self.myImage;
    [myview addSubview:imageV];
    
    
    // 缩放手势
    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
    [myview addGestureRecognizer:pinchGestureRecognizer];
    
    
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
    
    [myview addGestureRecognizer:pan];
    // Do any additional setup after loading the view.
}
//拖动手势

- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointZero inView:self.view];
      }

-(void)click{


    [self dismissViewControllerAnimated:YES completion:nil];
}
// 处理缩放手势
- (void) pinchView:(UIPinchGestureRecognizer *)sender
{
    //通过捏合手势的到缩放比率
    float scale = sender.scale;
    //得到该手势所作用的视图
    UIView *view = sender.view;
    //2D仿射变换函数中的缩放函数来实现视图的放大缩小
    
    //是在原有基础上来改变当前的视图
    //函数的第一个参数：现有的视图的transform值
    //第二个参数：x轴上的缩放比率
    //第三个参数:y轴上的缩放比率
    
    //是在视图最初的transform状态上改变，不管执行多少次，都是以该视图最初的transform状态为基础来改变
//    view.transform = CGAffineTransformMakeScale(scale, scale);
    
    
        view.transform = CGAffineTransformScale(view.transform, scale, scale);
//    每次捏合动作完毕之后，让此捏合值复原，使得它每次都是从100%开始缩放
    sender.scale = 1;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    [self dismissViewControllerAnimated:YES completion:nil];
//
//
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
